// 全局错误拦截

import { Catch, ExceptionFilter } from '@nestjs/common';
import type { ArgumentsHost, HttpException } from '@nestjs/common';
import type { Response, Request } from 'express';

@Catch()
export class HttpError implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const req = ctx.getRequest<Request>();
    const res = ctx.getResponse<Response>();
    const status = exception.getStatus();
    res.status(status).json({
      success: false,
      time: new Date(),
      data: exception,
      status,
      path: req.url,
    });
  }
}
